﻿package fui.FUIComponents{
	import flash.events.*;
	import flash.display.Sprite;
	import fl.controls.*;
	import fl.containers.ScrollPane;
	import flash.system.Security;
	import fui.FUIControllers.UISession;
	import fui.FUIData.PHPRemoting.*;
	import fui.FUIData.IDBClient;
	import fui.FUIData.DBClientV2;
	
	public class LoginView extends Sprite{
		private var _usess:UISession;
		private var usrLbl:Label;
		private var pwdLbl:Label;
		private var _warn:Label;
		private var _mainPane:ScrollPane;
		private var _usrInput:TextInput;
		private var _pwdInput:TextInput;
		private var _suBtn:Button;
		private var _loBtn:Button;
		private var dbc:IDBClient;
		
		public function LoginView(sess:UISession, startXPos:int, startYPos:int, dbCon:IDBClient){
			this._usess = sess;
			this.dbc = dbCon;
			x = startXPos;
			y = startYPos;
			buildView();
		}
		private function buildView():void{
			_mainPane = new ScrollPane();
			_mainPane.x = 1;
			_mainPane.y = 1;
			_mainPane.setSize(145, 115);
			
			usrLbl = new Label();
			pwdLbl = new Label();
			usrLbl.text = " User : ";
			usrLbl.width = 80;
			usrLbl.x = 10;//_startX + 10;
			usrLbl.y = 10;//_startY + 10;
			usrLbl.setSize(80, 18);
			_mainPane.addChild(usrLbl);
			pwdLbl.text = " password:";
			pwdLbl.width = 80;
			pwdLbl.x = 10;//_startX + 10;
			pwdLbl.y = 45;//_startY + 40;
			_mainPane.addChild(pwdLbl);
			_usrInput = new TextInput();
			_usrInput.x = 15;//_startX +95
			_usrInput.y = 28;//_startY + 10;
			_usrInput.setSize(100, 18);
			_mainPane.addChild(_usrInput);
			_pwdInput = new TextInput();
			_pwdInput.x = 15;//_startX + 95;
			_pwdInput.y = 63;//_startY + 40;
			_pwdInput.setSize(100, 18);
			_pwdInput.displayAsPassword = true;
			_mainPane.addChild(_pwdInput);
			_suBtn = new Button();
			_suBtn.x = 10;//_startX + 19;
			_suBtn.y = 100;//_startY + 85;
			_suBtn.label = "SignUp";
			_suBtn.setSize(80, 18);		
			_suBtn.addEventListener(MouseEvent.CLICK, signUpClick);
			//addChild(_suBtn);
			_loBtn = new Button();
			_loBtn.x = 10;//_startX + 112;
			_loBtn.y = 90;//_startY + 85;
			_loBtn.label = "Login";
			_loBtn.width = 118;
			_loBtn.height = 20;
			_loBtn.addEventListener(MouseEvent.CLICK, LoginClick);
			_mainPane.addChild(_loBtn);
			addChild(_mainPane);
		}
		function buildLoggedIn():void{
			//_mainPane.height = 115;
			
			usrLbl.x = 10;
			usrLbl.text = "User " + _usrInput.text + " \n is logged in!";	
			usrLbl.y = 10;
			usrLbl.setSize(190, 60);

			//_mainPane.addChild(muser);
			
			//_mainPane.removeChild(usrLbl);
			//_mainPane.removeChild(_loBtn);
			_mainPane.removeChild(_usrInput);
			_mainPane.removeChild(_pwdInput);
			_mainPane.removeChild(pwdLbl);
			if(_warn != null)
				_mainPane.removeChild(_warn);
			_loBtn.label = "Logout";
			_loBtn.removeEventListener(MouseEvent.CLICK, LoginClick);
			_loBtn.addEventListener(MouseEvent.CLICK, LogoutClick);
		}
		private function signUpClick(e:MouseEvent):void {
			trace("We should run Registration form");
			//var suv:SignUpView = new SignUpView(2,20);
			//_mainPane.visible = false;
			//addChild(suv);
			dispatchEvent(new Event(Event.OPEN));
		}
		private function LogoutClick(e:MouseEvent):void {
			//_loBtn.label = "Login";
			_loBtn.removeEventListener(MouseEvent.CLICK, LogoutClick);
			_mainPane.removeChild(_loBtn);
			_mainPane.removeChild(usrLbl);
			_usess.dropSession();
			//_loBtn.addEventListener(MouseEvent.CLICK, LoginClick);
			buildView();
			_usess.loginres(false);
			dispatchEvent(new Event("LOGGEDOUT"));
		}
		private function LoginClick(e:MouseEvent):void {
			trace("User: " + _usrInput.text + " PWD:" + _pwdInput.text);
			this.dbc.addEventListener(Event.COMPLETE, ifAuthed);
			this.dbc.CallStoredProcedure("verify_user_by_name", _usrInput.text, _pwdInput.text);
			
			
		}
		
		private function ifAuthed(event:Event):void
		{
			var dt:Object = this.dbc.CallResponse;
			trace (dt.rc);
			if(dt.rc == true)
			{
				_usess.startNewSession(dt.id, dt.name);
				_usess.profile = dt;
				_usess.loginres(true);
				trace("######## profile assigned to uisession");
				this.buildLoggedIn()
				dispatchEvent(new Event("LOGGEDIN"));
			}
			else
			{
				_warn = new Label();
				_warn.x = 60;
				_warn.y = 10;
				_warn.text = "Failed to Login";
				_mainPane.addChild(_warn);
				
			}
		}
		
	}
	
}